package com.pptv.ads.client.message;

import java.lang.reflect.Proxy;

import com.pptv.ads.client.JobInvocationHandler;
import com.pptv.ads.core.Executor;
import com.pptv.ads.core.model.Job;
import com.pptv.ads.core.model.Message;
import com.pptv.ads.core.model.Message.MessageType;
import com.pptv.ads.core.utils.SerializeUtil;

public class MessageHandler {

	public void handler(Message msg) throws Exception{
		
		if(MessageType.JOB == msg.getType()){
			try{
				Job job = SerializeUtil.deserialize(msg.getValue(), Job.class);
				Class<?> c = Class.forName(job.getExecutor());
				Object obj = c.newInstance();
				JobInvocationHandler handler = new JobInvocationHandler(obj);
				Executor executor = (Executor) Proxy.newProxyInstance(c.getClassLoader(), c.getInterfaces(), handler);
				executor.execute();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		
	}

	
}
